const mapClass = function mapClass(label) {
  return mapClass.map[label] || label;
};
mapClass.map = {
  '+/-': 'calc',
  '=': 'assign',
  '(': 'parenthesis',
  ')': 'parenthesis',
  '{': 'brace',
  '}': 'brace',
  '</>/<=/>=': 'compare',
  ';': 'semicolon',
};

const formatCode = function formatCode(list, code) {
  let start = 0;
  const arr = [];
  let token;
  for (token of list) {
    if (start !== token.start) {
      arr.push(code.substr(start, token.start - start));
    }
    const originStr = token.value != null ? token.value : token.type.label;
    const str = `<span class="token-${mapClass(token.type.label)}">${originStr}</span>`;
    arr.push(str);
    start = token.end;
  }
  return arr.join('');
};

export default formatCode;
